import { type ConfigEnv, defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// 路径别名
import path from "path";
const pathSrc = path.resolve(__dirname, "src");
// 自动引入
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

//mock
import { viteMockServe } from "vite-plugin-mock";
// https://vite.dev/config/
export default defineConfig(({ mode: ConfigEnv, command }) => {
  return {
    plugins: [
      vue(),
      AutoImport({
        // ...
        // 导入 Vue 函数，如：ref, reactive, toRef 等
        imports: ["vue", "@vueuse/core", "pinia", "vue-router", "vue-i18n"],
        resolvers: [
          // 导入 Element Plus函数，如：ElMessage, ElMessageBox 等
          ElementPlusResolver({ importStyle: "sass" }),
        ],
        // vueTemplate: true, // 是否在 vue 模板中自动导入
        dts: path.resolve(pathSrc, "types", "auto-imports.d.ts"), // 自动导入组件类型声明文件位置，默认根目录

        eslintrc: {
          enabled: false,
          filepath: "./.eslintrc-auto-import.json",
          globalsPropValue: true,
        },
        vueTemplate: true,
        // 导入函数类型声明文件路径 (false:关闭自动生成)
        // dts: false,
      }),
      Components({
        resolvers: [
          // 自动导入 Element Plus 组件
          ElementPlusResolver({ importStyle: "sass" }),
          // 自动注册图标组件
        ],
        dts: path.resolve(pathSrc, "types", "components.d.ts"), //  自动导入组件类型声明文件位置，默认根目录
        // 指定自定义组件位置(默认:src/components)
        dirs: ["src/components", "src/**/components"],
        // 导入组件类型声明文件路径 (false:关闭自动生成)
        // dts: false,
      }),
      viteMockServe({
        mockPath: "mock", // mock文件夹路径
        enable: command === "serve", // 只有开发环境才开启mock
      }),
    ],
    resolve: {
      alias: {
        "@": pathSrc,
      },
    },
  };
});
